﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		1. CaseTool
// Filename:	Reflection.cs
// 
// File Created:	21:17 30.05.2013 
// Last Changed:	18:11 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;

namespace SWE.CaseTool.Utils
{
	/// <summary>
	/// Contains some helper methods for c# reflection
	/// </summary>
	public static class Reflection
	{
		/// <summary>
		/// Returns the class attributes and the inherited class attributes of the given type.
		/// </summary>
		/// <typeparam name="T">The type where the attributes are loaded from</typeparam>
		/// <param name="type">The attribute type.</param>
		/// <returns>a list of all attibutes with the same type than "type"</returns>
		public static List<T> GetAttributes<T>(this Type type) where T : Attribute { return type.GetCustomAttributes(true).OfType<T>().ToList(); }

		/// <summary>
		/// Returns the class attributes and the inherited class attributes of the given type. Returns only 1
		/// Attribute or null.
		/// </summary>
		/// <typeparam name="T">The type where the attributes are loaded from</typeparam>
		/// <param name="type">The attribute type.</param>
		/// <returns>The first attribute or null, it there are not attributes with the searched type</returns>
		public static T GetAttribute<T>(this Type type) where T : Attribute
		{
			var atts = type.GetAttributes<T>();
			if (atts.Count == 0)
			{
				return null;
			}
			return atts[0];
		}
	}
}